package com.shuw.com.beingfortextproject.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.shuw.com.beingfortextproject.R;
import com.shuw.com.beingfortextproject.adapter.ViewPagerAdapter;
import com.shuw.com.beingfortextproject.base.BaseActivity;
import com.shuw.com.beingfortextproject.widget.ScaleCircleNavigator;
import com.shuw.com.beingfortextproject.widget.ViewPagerHelper;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

// 首次打开导航页

public class ViewPagerActivity extends BaseActivity {

	@BindView(R.id.viewPager)
	ViewPager viewPager;
	@BindView(R.id.titleTv)
	TextView titleTv;
	@BindView(R.id.ratingBar)
	RatingBar ratingBar;
	@BindView(R.id.jumpTv)
	TextView jumpTv;
	@BindView(R.id.indicator)
	MagicIndicator indicator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_pager);

		ButterKnife.bind(this);

		initView();
	}

	private ArrayList<View> viewList = new ArrayList<>();
	private void initView() {
		//
		LayoutInflater inflater = getLayoutInflater();
		View view1 = inflater.inflate(R.layout.layout_first_pager, null);
		View view2 = inflater.inflate(R.layout.layout_second_pager, null);
		View view3 = inflater.inflate(R.layout.layout_third_pager, null);
		viewList.add(view1);
		viewList.add(view2);
		viewList.add(view3);
		ratingBar.setNumStars(viewList.size());
		ViewPagerAdapter adapter = new ViewPagerAdapter(viewList);
		viewPager.setAdapter(adapter);

		viewPager.setCurrentItem(0);
		titleTv.setText("Current page is ---> " + 0);
		ratingBar.setRating(1);
		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				titleTv.setText("Current page is ---> " + position);
				ratingBar.setRating(position + 1);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		initIndicator();
	}

	private void initIndicator() {
		ScaleCircleNavigator scaleCircleNavigator = new ScaleCircleNavigator(this);
		scaleCircleNavigator.setCircleCount(viewList.size());
		scaleCircleNavigator.setNormalCircleColor(Color.GRAY);
		scaleCircleNavigator.setSelectedCircleColor(Color.DKGRAY);
		scaleCircleNavigator.setCircleClickListener(index ->
				viewPager.setCurrentItem(index)
		);
		indicator.setNavigator(scaleCircleNavigator);
		ViewPagerHelper.bind(indicator,viewPager);
	}

	@OnClick(R.id.jumpTv)
	public void onClick() {
		finish();
	}
}
